package com.example.t1p3;

import java.util.LinkedList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private Game game = new Game();
	
	private final int NEW_GAME_CODE = 0;
	private final int NEW_GOAL_CODE = 1;	
	private final int END_PERIODE_CODE = 2;	
	private final int VIEW_TEAM_CODE = 3;
	public final String GAME_PACKAGE = "Game";	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void viewTeams1(View view) {
    	IntentHelper.addObjectForKey(game.getTeam1(), "team");
    	Intent intent = new Intent(this, ViewTeamsActivity.class);
    	startActivity(intent);
    }
    
    public void viewTeams2(View view) {
    	IntentHelper.addObjectForKey(game.getTeam2(), "team");
    	Intent intent = new Intent(this, ViewTeamsActivity.class);
    	startActivity(intent);
    }
    
    public void newGame(View view) {
    	Intent intent = new Intent(this, NewGameActivity.class);
    	startActivityForResult(intent, NEW_GAME_CODE);
    }
    
    public void newGoal(View view) {
    	IntentHelper.addObjectForKey(game, "gameNewGoal");
    	
    	Intent intent = new Intent(this, NewGoalActivity.class);
    	startActivityForResult(intent, NEW_GOAL_CODE);
    }
    
    public void endPeriod(View view) {
    	LinkedList<Goal> listGoal = getGoalFromThisPeriod();
    	
    	IntentHelper.addObjectForKey(listGoal, "listGoal");
    	IntentHelper.addObjectForKey(game.getPeriod(), "period");
    	IntentHelper.addObjectForKey(game.getListGoal(), "listGoalStar");
    	
    	Intent intent = new Intent(this, EndPeriodActivity.class);
    	startActivityForResult(intent, END_PERIODE_CODE);
    	
    }
    
    private LinkedList<Goal> getGoalFromThisPeriod() {
		LinkedList<Goal> listGoal = new LinkedList<Goal>();
		
		for(int i=0; i < game.getListGoal().size(); i++){
			if(game.getListGoal().get(i).getPeriod() == game.getPeriod()){
				listGoal.add(game.getListGoal().get(i));
			}
		}
		
		return listGoal;
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == NEW_GAME_CODE) {
    		game = (Game) IntentHelper.getObjectForKey("gameFromNewGame");
        	((Button) findViewById(R.id.btnGoal)).setEnabled(true);
        	((Button) findViewById(R.id.btnViewTeam1)).setEnabled(true);
        	((Button) findViewById(R.id.btnViewTeam2)).setEnabled(true);
        	((Button) findViewById(R.id.btnEndPeriod)).setEnabled(true);
        	((TextView)findViewById (R.id.textTeam1)).setText(""+game.getTeam1().getNom());
        	((TextView)findViewById (R.id.textTeam2)).setText(""+game.getTeam2().getNom());
    		((TextView)findViewById (R.id.textPeriodNumber)).setText(""+game.getPeriod());
        }else  if (resultCode == Activity.RESULT_OK && requestCode == NEW_GOAL_CODE) {
    		Goal goal = (Goal) IntentHelper.getObjectForKey("goal");
    		game.score(goal);
    		
        	((TextView)findViewById (R.id.textPointTeam1)).setText(""+game.getTeam1().getScore());
        	((TextView)findViewById (R.id.textPointTeam2)).setText(""+game.getTeam2().getScore());

        }else  if (resultCode == Activity.RESULT_OK && requestCode == END_PERIODE_CODE) {
        	if(game.getPeriod() !=3){
        		game.setPeriod(game.getPeriod() +1);
        		((TextView)findViewById (R.id.textPeriodNumber)).setText(""+game.getPeriod());
        	}else{
            	((Button) findViewById(R.id.btnGoal)).setEnabled(false);
            	((Button)findViewById (R.id.btnEndPeriod)).setEnabled(false);
        	}
        }
    }
}
